package com.tool;
import java.util.Locale;
import java.util.MissingResourceException;
import java.util.ResourceBundle;

/**
 * 多语言实现基础
 * 
 * @author Evan.Hu
 * 
 */
public class ResourceBundleHelper {
	private ResourceBundle resourceBundle;
	private String baseName;
	private Locale locale;

	public ResourceBundleHelper(String baseName) {
		this(baseName, null);
	}

	public ResourceBundleHelper(String baseName, Locale l) {
		this.resourceBundle = null;

		if (l == null) {
			// 多语言实现的基础
			// l = LocaleUtils.getDefaultLocale();
			l = Locale.getDefault();
		}
		this.baseName = baseName;
		this.locale = l;
	}

	public String getString(String key) {
		String value = null;
		try {
			value = getResourceBundle().getString(key);
		} catch (MissingResourceException e) {
			System.out.println("java.util.MissingResourceException: Couldn't find value for: " + key);
		}
		if (value == null) {
			value = "Could not find resource: " + key + "  ";
		}
		return value;
	}

	public char getMnemonic(String key) {
		return (getString(key)).charAt(0);
	}

	private ResourceBundle getResourceBundle() {
		if (resourceBundle == null) {
			resourceBundle = ResourceBundle.getBundle(baseName, locale);
		}
		return resourceBundle;
	}
}
